package com.ethan.cj.ssm.service.impl;

import com.ethan.cj.ssm.dao.PeopleDao;
import com.ethan.cj.ssm.entity.People;
import com.ethan.cj.ssm.entity.Role;
import com.ethan.cj.ssm.service.PeopleService;
import com.ethan.cj.ssm.service.RoleService;
import com.ethan.cj.ssm.util.PageBean;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员查询service层实现
 */
@Service
public class PeopleServiceImpl implements PeopleService {

    @Autowired
    private PeopleDao peopleDao;
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     * @param pageBean
     * @param param
     * @return
     */
    @Override
    public Map<Object, Object> pagePeopleList(PageBean pageBean, Map<Object, Object> param) {
        Map<Object, Object> result=new HashMap<>();
        if (param == null) {
            param = new HashMap<>();
        }
        param.put("pb",pageBean);
        List<People>list=peopleDao.pagePeopleList(param);
        List<Role> roles=roleService.list();
        int count=selectCount(param);
        result.put("code",0);//状态码
        result.put("msg",""); //附带消息
        result.put("count",count);
        result.put("list",roles);
        result.put("data",list);
        return result;
    }

    /**
     * 查询条数
     * @param param
     * @return
     */
    @Override
    public int selectCount(Map<Object, Object> param) {
        return peopleDao.selectCount(param);
    }

    /**
     * 新增人员
     * @param param
     * @return
     */
    @Override
    public Map<Object, Object> insertPeople(String param) {

        People people= null;
        try {
            people = new ObjectMapper().readValue(param, People.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<Object, Object> result=new HashMap<>();
        if (people != null) {
            people.setPassword("123456");
            people.setAddTime(new Date());
            people.setLoginNum(0);
            people.setStatus(0);
            int rst = peopleDao.insertPeople(people);
            if (rst > 0) {
                result.put("code", 1);
                result.put("msg", "增加成功！");
            } else {
                result.put("code", 0);
                result.put("msg", "增加失败");
            }
        } else {
            result.put("code", 0);
            result.put("msg", "人员信息JSON解析失败！");
        }

        return result;
    }

    /**
     * 按姓名查询人员
     */
    @Override
    public People selectPeopleByName(String pname) {
        People people=peopleDao.selectPeopleByName(pname);
        return people;
    }

    /**
     * 按手机号查询人员
     */
    @Override
    public People selectPeopleByPhonenum(String phonenum) {
        People people=peopleDao.selectPeopleByPhonenum(phonenum);
        return people;
    }

    /**
     * 通过姓名查询人员不是当前id
     * @param pname
     * @param pid
     * @return
     */
    @Override
    public People selectPeopleName(String pname, int pid) {
        Map<String,Object> map=new HashMap<>();
        map.put("pname",pname);
        map.put("pid",pid);
        return peopleDao.selectPeopleName(map);
    }

    /**
     * 通过手机号查询人员不是当前id
     * @param phonenum
     * @param pid
     * @return
     */
    @Override
    public People selectPeoplePhoneNum(String phonenum, int pid) {

        Map<String,Object> map=new HashMap<>();
        map.put("phonenum",phonenum);
        map.put("pid",pid);
        return peopleDao.selectPeoplePhoneNum(map);
    }

    /**
     * 根据id查询
     * @param pid
     * @return
     */
    @Override
    public People selectPeopleByPid(int pid) {

        return peopleDao.selectPeopleByPid(pid);
    }

    /**
     * 修改人员信息
     * @param param
     * @return
     */
    @Override
    public Map<Object, Object> updatePeople(String param) {
        People people= null;
        try {
            people = new ObjectMapper().readValue(param, People.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<Object, Object> result=new HashMap<>();
        if (people != null) {
            int rst = peopleDao.updatePeople(people);
            if (rst > 0) {
                result.put("code", 1);
                result.put("msg", "修改成功！");
            } else {
                result.put("code", 0);
                result.put("msg", "修改失败");
            }
        } else {
            result.put("code", 0);
            result.put("msg", "人员信息JSON解析失败！");
        }

        return result;
    }

    /**
     * 修改人员状态
     */
    @Override
    public Map<Object, Object> updateStatus(int pid, int status) {
        if(status==0){
            status=1;
        }else {status=0;}

        Map<Object, Object> result=new HashMap<>();
        Map<String, Object> param=new HashMap<>();
        param.put("pid",pid);
        param.put("status",status);
        int rst = peopleDao.updateStatus(param);
        if (rst > 0) {
            result.put("code", 1);
            result.put("msg", "修改成功！");
        } else {
            result.put("code", 0);
            result.put("msg", "修改失败");
        }
        return result;
    }

    /**
     * 根据id删除人员信息
     * @param pid
     * @return
     */
    @Override
    public Map<Object, Object> deletePeppleById(int pid) {
        Map<Object, Object> result=new HashMap<>();

            int rst = peopleDao.deletePeppleById(pid);
            if (rst > 0) {
                result.put("code", 1);
                result.put("msg", "删除成功！");
            } else {
                result.put("code", 0);
                result.put("msg", "删除失败");
            }
        return result;
    }

    /**
     * 修改登录信息
     */
    @Override
    public int updateLogin(People people) {

        return peopleDao.updateLogin(people);
    }

}
